package utils;

import java.util.*;

import org.w3c.dom.*;

public class XMLReader {
	private final static int parseInt(String str) {
		if (str != null) {
			str = str.trim();
			int v = 0;
			for (int i = 0; i < str.length(); i++) {
				char ch = str.charAt(i);
				if (ch >= '0' && ch <= '9') {
					v = v*10 + ch - '0';
				} else {
					return 0;
				}
			}
			return v;
		}
		return 0;
	}
	
	public final static Vector<Node> getNodeList(Element node, String key) {
		if (node != null && key != null) {
			NodeList list = node.getElementsByTagName(key);
			if (list != null && list.getLength() > 0) {
				Node temp = list.item(0);
				if (temp != null) {
					list = temp.getChildNodes();
					if (list != null) {
						Vector<Node> nodes = new Vector<Node>();
						for (int i = 0; i < list.getLength(); i++) {
							if (list.item(i).getNodeType() == Node.ELEMENT_NODE) {
								nodes.add(list.item(i));
							}
						}
						return nodes;
					}
				}
			}
		}
		return null;
	}
	
	public final static Element getNodeElement(Element node, String key) {
		if (node != null && key != null) {
			NodeList list = node.getElementsByTagName(key);
			if (list != null && list.getLength() > 0) {
				return (Element)list.item(0);
			}
		}
		return null;
	}
	
	public final static String getStringForKey(Element node, String key, String defaultValue) {
		if (node != null && key != null) {
			NodeList list = node.getElementsByTagName(key);
			if (list != null) {
				if (list.getLength() > 0) {
					Node n = list.item(0);
					if (n != null) {
						n = n.getFirstChild();
						if (n != null) {
							return n.getNodeValue();
						}
					}
				}
			}
		}
		return defaultValue;
	}
	
	public final static int getIntForKey(Element node, String key, int defaultValue) {
		if (node != null && key != null) {
			NodeList list = node.getElementsByTagName(key);
			if (list != null) {
				if (list.getLength() > 0) {
					Node n = list.item(0);
					if (n != null) {
						n = n.getFirstChild();
						if (n != null) {
							return parseInt(n.getNodeValue());
						}
					}
				}
			}
		}
		return defaultValue;
	}
	
	public final static String getStringForIndex(NodeList list, int index, String defaultValue) {
		if (list != null && index >= 0 && index < list.getLength()) {
			if (list.getLength() > 0) {
				Node n = list.item(0);
				if (n != null) {
					n = n.getFirstChild();
					if (n != null) {
						return n.getNodeValue();
					}
				}
			}
		}
		return defaultValue;
	}
	
	public final static int getIntForIndex(NodeList list, int index, int defaultValue) {
		if (list != null && index >= 0 && index < list.getLength()) {
			if (list.getLength() > 0) {
				Node n = list.item(0);
				if (n != null) {
					n = n.getFirstChild();
					if (n != null) {
						return parseInt(n.getNodeValue());
					}
				}
			}
		}
		return defaultValue;
	}
	
	public final static Dictionary<String, Object> convert2Dictionary(Element node) {
		Dictionary<String, Object> dic = new Hashtable<String, Object>();
		NodeList childNodes = node.getChildNodes();
		for(int j=0;j<childNodes.getLength();j++)  
        {  
                Node childNode = childNodes.item(j);  
                if (Node.ELEMENT_NODE == childNode.getNodeType()) {
                	String name = childNode.getNodeName();
                	NodeList tempList = childNode.getChildNodes();
                	if (tempList != null) {
                		if (tempList.getLength() == 1) {
                        	String value = null;
                    		Node value_node = tempList.item(0);
                    		if (value_node != null) {
                    			value = value_node.getNodeValue();
                    			dic.put(name, value);
                    		}
                		} else if (tempList.getLength() >  1) {
                			Dictionary<String, Object> sub = XMLReader.convert2Dictionary((Element) childNode);
                			dic.put(name, sub);
                		}
                	}
                }
        }    
		return dic;
	}
	
	public final static Vector<Dictionary<String, Object>> convert2Array(Vector<Node> list) {
		Vector<Dictionary<String, Object>> arr = new Vector<Dictionary<String, Object>>();
		for (int i = 0; i < list.size(); i++) {
			Node obj = list.elementAt(i);
			Dictionary<String, Object> sub = XMLReader.convert2Dictionary((Element)obj);
			sub.put("item-key", obj.getNodeName());
			arr.add(sub);
		}
		return arr;
	}
	
	public final static Vector<String> convert2StringArray(Vector<Node> list) {
		Vector<String> arr = new Vector<String>();
		for (int i = 0; i < list.size(); i++) {
			Node obj = list.elementAt(i);
			NodeList tempList = obj.getChildNodes();
			if (tempList != null) {
        		if (tempList.getLength() == 1) {
                	String value = null;
            		Node value_node = tempList.item(0);
            		if (value_node != null) {
            			value = value_node.getNodeValue();
            			arr.add(value);
            		}
        		}
        	}
		}
		return arr;
	}
}
